/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.eurotrader.push;

import br.com.eurotrader.utils.AcoesEstrategias;
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Stateless;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.inject.Named;
import org.icefaces.application.PushRenderer;

/**
 *
 * @author Edmilson
 */
//@ApplicationScoped
//@Stateful
@Stateless
@Named
public class CentralPush implements Serializable {

    private static final long serialVersionUID = 1L;
    
    public CentralPush() {
    }

    @PostConstruct
    public void initialize() {
        if (FacesContext.getCurrentInstance() != null) {
            AcoesEstrategias.pr = PushRenderer.getPortableRenderer(FacesContext.getCurrentInstance());
        }
    }

    @PreDestroy
    public void destroy() {
        System.out.println("destroindo e parando o push timer");
    }



    public void chamarPush(ActionEvent event) {
        if (AcoesEstrategias.pr == null) {
            if (FacesContext.getCurrentInstance() != null) {
                AcoesEstrategias.pr = PushRenderer.getPortableRenderer(FacesContext.getCurrentInstance());
                AcoesEstrategias.pr.render(AcoesEstrategias.APPLICATION_PUSH_GROUP);
            }
        } else{
            AcoesEstrategias.pr.render(AcoesEstrategias.APPLICATION_PUSH_GROUP);
        }


    }
    
    public void iniciarPush(ActionEvent event) {
        if (AcoesEstrategias.pr == null) {
            if (FacesContext.getCurrentInstance() != null) {
                AcoesEstrategias.pr = PushRenderer.getPortableRenderer(FacesContext.getCurrentInstance());
                AcoesEstrategias.pr.render(AcoesEstrategias.APPLICATION_PUSH_GROUP);
            }
        } 
    }

    public String getGrupo() {
        return AcoesEstrategias.APPLICATION_PUSH_GROUP;
    }
}
